import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;

import local.util.MyStringFormat;
import local.util.MyPrint;

public class GenerateReport {    

    private Vector IndexRecordVector = new Vector ();
    DecimalFormat twoDigits = new DecimalFormat("0.00");
    MyStringFormat mf = new MyStringFormat();

    public static void main (String args[]) {

	GenerateReport report = new GenerateReport ();

	if(args.length>0 && args[0].equals("-help")) {
	    report.printHelpMessage();
	}

	Vector t_head_v   = new Vector();
	Vector t_record_v = new Vector();
	Vector record_v = new Vector ();
	Hashtable t_head_h = new Hashtable ();
	String my_query_string ="SELECT * FROM volume_record ORDER BY Exchange, Date";

	DataBaseQuery my_query = new DataBaseQuery ();

	my_query.setQueryString (my_query_string);
	my_query.lookup (my_query_string);

	t_head_v    = my_query.getTableHeadVector();
	t_record_v  = my_query.getTableRecordVector();
	MyPrint.printLine("=",120);

	report.setIndexRecordVector (t_record_v);
	record_v = report.getIndexRecordVector();
	report.printReport(record_v,"NASDAQ");

	MyPrint.printLine("=",120);
	report.printReport(record_v,"NYSE");
	MyPrint.printLine("=",120);
	report.printReport(record_v,"AMEX");
	MyPrint.printLine("=",120);
	report.printReport(record_v,"BULLBD");
	MyPrint.printLine("=",120);
    }

    private void printHelpMessage () {

	System.out.println("========================================");
	System.out.println("Generate Report using db data");
	System.out.println("[1] List Index volume by date");
	System.out.println("========================================");
	System.exit(0);
    }

    public void printReport( Vector input_r_v, String exchange) {

	String ex = exchange;

	long volume = 0;
	long volume_up = 0;
	long volume_down = 0;
	long volume_unchange = 0;

	int total_stock             =0;
	int total_stock_up          =0;
	int total_stock_down        =0;
	int total_stock_unchange    =0;

	IndexRecord index_record = new IndexRecord(); 
	NasdaqReport naz = new NasdaqReport();
	naz.setNazRecordVector("05/18/2002");

	Enumeration enum1 = input_r_v.elements();
	while ( enum1.hasMoreElements()) { 
	    index_record = (IndexRecord)enum1.nextElement();

	    if (index_record.getExchange().equals(ex)) {
		System.out.print(index_record.getExchange()+"\t");
		System.out.print(mf.leftIndent(index_record.getDate().replaceAll("00:00:00",""),10,' ')+"  ");
	
		volume          = Long.parseLong(index_record.getTotalVolume().replaceAll(",",""));
		volume_up       = Long.parseLong(index_record.getTotalVolumeUp().replaceAll(",",""));
		volume_down     = Long.parseLong(index_record.getTotalVolumeDown().replaceAll(",",""));
		volume_unchange = Long.parseLong(index_record.getTotalVolumeUnchange().replaceAll(",",""));

		System.out.print(volume+"\t");
		System.out.print(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
		System.out.print(mf.rightIndent(volume_up, 12, ' ')+" "+"("+
				 mf.rightIndent(volume_up*100/volume,2,' ')+"%)  ");
		System.out.print(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
	
		System.out.print(mf.rightIndent(volume_down,12,' ')+" "+"("+
				 mf.rightIndent(volume_down*100/volume,2,' ')+"%)  ");
		System.out.print(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
	
		System.out.print(mf.rightIndent(volume_unchange,10,' ') +" "+"("+
				   mf.rightIndent(volume_unchange*100/volume,2,' ')+"%)  ");
		System.out.println(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
	    }
	}

	MyPrint.printLine(".",120);

	Enumeration enum2 = input_r_v.elements();
	while ( enum2.hasMoreElements()) { 
	    index_record = (IndexRecord)enum2.nextElement();
	    if (index_record.getExchange().equals(ex)) {
		System.out.print(index_record.getExchange()+"\t");
		System.out.print(mf.leftIndent(index_record.getDate().replaceAll("00:00:00",""),10,' ')+"  ");	
		total_stock          =   index_record.getTotalStock();
		total_stock_up       =   index_record.getTotalStockUp();
		total_stock_down     =   index_record.getTotalStockDown();
		total_stock_unchange =   index_record.getTotalStockUnchange();

		System.out.print(mf.rightIndent(total_stock,10,' ')+"  ");
	
		System.out.print(mf.rightIndent(total_stock_up,10,' ')+" "+"("+
				 mf.rightIndent(total_stock_up*100/total_stock,2,' ')+"%)  ");
		System.out.print(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
		System.out.print(mf.rightIndent(total_stock_down,10,' ')+" "+"("+
				 mf.rightIndent(total_stock_down*100/total_stock,2,' ')+"%)  ");
		System.out.print(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
		System.out.print(mf.rightIndent(total_stock_unchange,10,' ')+" "+"("+
				   mf.rightIndent(total_stock_unchange*100/total_stock,2,' ')+"%)  ");
		System.out.println(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
	    }
	}
    }

    public void printTotalReport( Vector input_r_v, String exchange) {

	String ex = exchange;
	long total_volume = 0;
	long total_volume_up = 0;
	long total_volume_down = 0;
	long total_volume_unchange = 0;

	int total_stock             =0;
	int total_stock_up          =0;
	int total_stock_down        =0;
	int total_stock_unchange    =0;

	long volume = 0;
	long volume_up = 0;
	long volume_down = 0;
	long volume_unchange = 0;

	int stock             =0;
	int stock_up          =0;
	int stock_down        =0;
	int stock_unchange    =0;

	IndexRecord index_record = new IndexRecord(); 
	NasdaqReport naz = new NasdaqReport();
	naz.setNazRecordVector("05/18/2002");

	Enumeration enum1 = input_r_v.elements();
	while ( enum1.hasMoreElements()) { 
	    index_record = (IndexRecord)enum1.nextElement();

	    if (index_record.getExchange().equals(ex)) {
		System.out.print(index_record.getExchange()+"\t");
		System.out.print(mf.leftIndent(index_record.getDate().replaceAll("00:00:00",""),10,' ')+"  ");
		
		volume          = Long.parseLong(index_record.getTotalVolume().replaceAll(",",""));
		volume_up       = Long.parseLong(index_record.getTotalVolumeUp().replaceAll(",",""));
		volume_down     = Long.parseLong(index_record.getTotalVolumeDown().replaceAll(",",""));
		volume_unchange = Long.parseLong(index_record.getTotalVolumeUnchange().replaceAll(",",""));

		total_volume = volume + total_volume;
		total_volume_up = 100*volume_up/total_volume + total_volume_up; 
		total_volume_down = 100*volume_down/total_volume + total_volume_down; 
		total_volume_unchange = 100*volume_unchange/total_volume + total_volume_unchange;

		System.out.print(mf.rightIndent(total_volume_up, 12, ' '));
		System.out.print(mf.rightIndent(total_volume_down,12,' '));
		System.out.print(mf.rightIndent(total_volume_unchange,10,' '));
		System.out.println(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
		
	    }
	}

	MyPrint.printLine(".",120);

	Enumeration enum2 = input_r_v.elements();
	while ( enum2.hasMoreElements()) { 
	    index_record = (IndexRecord)enum2.nextElement();
	    if (index_record.getExchange().equals(ex)) {
		System.out.print(index_record.getExchange()+"\t");
		System.out.print(mf.leftIndent(index_record.getDate().replaceAll("00:00:00",""),10,' ')+"  ");	
		stock          =   index_record.getTotalStock();
		stock_up       =   index_record.getTotalStockUp();
		stock_down     =   index_record.getTotalStockDown();
		stock_unchange =   index_record.getTotalStockUnchange();

		System.out.print(mf.rightIndent(stock,10,' ')+"  ");
	
		System.out.print(mf.rightIndent(stock_up,10,' ')+" "+"("+
				 mf.rightIndent(stock_up*100/stock,2,' ')+"%)  ");
		System.out.print(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
		System.out.print(mf.rightIndent(stock_down,10,' ')+" "+"("+
				 mf.rightIndent(stock_down*100/stock,2,' ')+"%)  ");
		System.out.print(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
		System.out.print(mf.rightIndent(stock_unchange,10,' ')+" "+"("+
				   mf.rightIndent(stock_unchange*100/stock,2,' ')+"%)  ");
		System.out.println(mf.rightIndent(naz.getNazChange(index_record.getDate()),5,' '));
	    }
	}
    }

    public void setIndexRecordVector (Vector vv) {

	Vector rv = new Vector();
	Vector record_vector = new Vector();
   
	Enumeration enum = vv.elements();
	while ( enum.hasMoreElements()) { 
	    rv = (Vector)enum.nextElement();
	    IndexRecord index_record = new IndexRecord ();
	    index_record.setIndexRecord(rv);
	    IndexRecordVector.add(index_record.getIndexRecord());
	}
    }      

    public Vector getIndexRecordVector() {
	return IndexRecordVector;
    }
}








